<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../dashboard/mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: window.innerHeight-230+'px'
    },
    chartData: {
      type: Object
    }
  },
  data() {
    return {
      chart: null,
      lightColors:["#22c119","#ffba0c","#10a0ff","#de94de","#d83006"],
      darkColors:["#d4ffc7","#fffdd8","#dff6fc","#f2e2ff","#fcbbbb"]
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        console.log('val')
        console.log(val)
        this.setOptions(val)
      }
    }
  },
  methods: {
    initChart() {
      this.width = "1200px !important"
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(chartData) {
      console.log(chartData)
      let series = []
      for(var i=0;i<chartData.ydata.length;i++){
        var ser =  {
          name: chartData.legend[i],
          type: 'bar',
          // stack: 'vistors',
          barWidth: 18,
          data: chartData.ydata[i],
          itemStyle: {
            color: {//渐变色
               x: 0,
               y: 0,
               x2: 0,
               y2: 1,
               type: 'linear',
               global: false,
               colorStops: [
                  {
                     offset: 0,
                     color: this.lightColors[i],
                  },
                  {
                     offset: 1,
                     color: this.darkColors[i],
                  },
               ],
            },
          },
        }
        series.push(ser)
      }
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
            right: 'center',
            itemWidth: 20,
            itemHeight: 16,
            // 两个之间的间隙大小
            textStyle: {
               // 图例文字的样式
               color: '#18191B',
               fontSize: 14,
            },
         },
        grid: {
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: chartData.xdata,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel: {
            fontSize: 14,
            color: "#18191B"
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          },
          axisLabel: {
            fontSize: 14,
            color: "#18191B"
          }
        }],
        series:series
      },true)
    }
  }
}
</script>
